package com.songshuai.webapi.baseapi.controller;

import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.service.BaseDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Slf4j
@Api(value="字典信息")
public class BaseDictionaryController {

    @Autowired
    private BaseDictionaryService baseDictionaryService;

    @ApiOperation(value="根据ID获取字典信息")
    @RequestMapping(value = "/api/dictionary/getDictionaryById", method = RequestMethod.GET)
    public QueryResponse<DictionaryDto> getDictionaryById(@RequestParam Integer dicId){
        try{
            DictionaryDto dictionaryDto=baseDictionaryService.getDictionaryById(dicId);
            if(dictionaryDto==null){
                return QueryResponse.setResData(dictionaryDto, ResultStatus.FAIL,"");
            }
            return  QueryResponse.setResData(dictionaryDto);
        }catch (Exception ex){
            log.error("getDictionaryById",ex);
            return QueryResponse.setResData(null, ResultStatus.FAIL,ex.getMessage());
        }
    }

    @ApiOperation(value="根据Type获取字典信息")
    @RequestMapping(value = "/api/dictionary/getDictionaryByType", method = RequestMethod.GET)
    public QueryResponse<List<DictionaryDto>> getDictionaryByType(@RequestParam Integer typeId){
        try{
            List<DictionaryDto> dictionaryDtoList=baseDictionaryService.getDictionaryByType(typeId);
            return  QueryResponse.setResData(dictionaryDtoList);
        }catch (Exception ex){
            log.error("getDictionaryByType",ex);
            return QueryResponse.setResData(null, ResultStatus.FAIL,ex.getMessage());
        }
    }

    @ApiOperation(value="根据多个id获取字典信息")
    @RequestMapping(value = "/api/dictionary/getDictionaryByIds", method = RequestMethod.POST)
    public QueryResponse<List<DictionaryDto>> getDictionaryByIds(@RequestBody List<Integer> dicIds){
        try{
            List<DictionaryDto> dictionaryDtoList=baseDictionaryService.getDictionaryByIds(dicIds);
            return  QueryResponse.setResData(dictionaryDtoList);
        }catch (Exception ex){
            log.error("getDictionaryByType",ex);
            return QueryResponse.setResData(null, ResultStatus.FAIL,ex.getMessage());
        }
    }
}
